<?php
class CoursesController extends AppController {
	function backend_index() {
		$this->set ( 'courses', $this->Course->find ( 'all' ) );
	}
	function backend_new() {
		$this->__formProccessing ();
	}
	function backend_edit($id = null) {
		$this->__formProccessing ( $id );
	}
	function backend_delete($id) {
		if ($this->Course->delete ( $id ))
			$this->Session->setFlash ( 'El registro fue eliminado con exito.', 'ok' );
		else
			$this->Session->setFlash ( 'Hubo un error al eliminar el registro.', 'error' );
		$this->redirect ( array (
				'action' => 'index' 
		) );
	}
	private function __formProccessing($id = null) {
		if (! empty ( $this->request->data )) {
			if (! $id)
				$this->Course->create ();
			if ($this->Course->save ( $this->request->data )) {
				$this->Session->setFlash ( 'El registro fue guardado con exito.', 'ok' );
				$this->redirect ( array (
						'action' => 'index' 
				) );
			} else {
				$this->Session->setFlash ( 'Hubo un error al procesar el registro.', 'error' );
			}
		}
		if (is_numeric ( $id ))
			$this->request->data = $this->Course->findById ( $id );
	}
}
?>